'use strict';
module.exports = function (grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        concurrent: {
            tasks: ['shell:start', 'tools'],
            options: {
                logConcurrentOutput: true
            }
        },
        shell: {
            start: {
                command: 'node ./index.js &'
            },
            debugStart: {
                commond: 'DEBUG=proxy node ./index.js'
            }
        },
        sprite: {
            all: {
                src: 'app/img/sprites/*.png',
                destImg: 'app/img/sprites.png',
                
                destCSS: 'app/less/core/sprite.less',
                cssFormat: 'less', 
                
                padding: 15,
                algorithm: 'binary-tree',
            }
        },
        
        
        less: {
            options: {
                expand: true,
                sourceMap: true
            },
            dev: {
                files: {
                    'app/css/main.css': ['app/less/main.less'],
                    'app/css/app.css': ['app/less/app.less']
                }
            }
        }, 
        watch: {
            gruntfile: {
                files: 'Gruntfile.js '
            },
            
            less: {
                files: ['app/less/**/*.less'],
                tasks: ['less']
            },
            jsx: {
                files: ['app/js/components/**/*.jsx'],
                tasks: ['react']
            },
            options: {
                spawn: true
            }
            
        },
        browserSync: {
            files: {
                src: ['app/*.html', 'app/css/*.css','app/css/*.css.map', 'app/js/**/*.js', 'app/css/main.css']
            },
            options: {
                watchTask: true,
                ghostMode: {
                    clicks: true,
                    scroll: true,
                    links: true,
                    forms: true
                },
                // server: {
                //     baseDir: 'app'
                // }
                proxy: 'localhost:3999'
            }
        },
        react: {
            files: {
                  expand: true,
                  cwd: 'app/js/components',
                  dest: 'app/js/components/',
                  src: ['**/*.jsx'],
                  ext: '.js'
                }
        },
        //项目发布
        clean:["dest/js/components","dest/js/lib"],
        lint: {
            beforeconcat: [
                "app/js/**/*.js"
            ]
        },
        jshint: {
            options: {
                eqeqeq: true,
                trailing: true
            },
            files: ['Gruntfile.js', 'app/js/**/*.js']
        },
        requirejs: {
            compile: {
                options: {
                    baseUrl: ["dest/js"],
                    name: "main", // assumes a production build using almond
                    out: "dest/js/main.js",
                    paths: {
                        jquery: 'vendor/jquery/dist/jquery.min',
                        director: 'vendor/director/build/director.min',
                        highcharts: 'vendor/highcharts/highcharts',
                        react: 'vendor/react/react.min',
                        reactAddons: 'vendor/react/react-with-addons.min',
                        JSXTransformer: 'vendor/react/JSXTransformer',
                        cookieStorage: 'vendor/cookie-storage/dist/cookie-storage',
                        json2: 'vendor/json/json2',
                        xdate: 'vendor/xdate/src/xdate',
                        moment: 'vendor/moment/min/moment.min',
                        jPages: 'vendor/jPages/js/jPages',
                        numberFormat: 'vendor/number-format.js/lib/format.min',
                        bootstrap:'vendor/bootstrap/dist/js/bootstrap.min',
                        datetimepicker:'vendor/bootstrap-datetimepicker/js/bootstrap-datetimepicker',
                        datetimepickerLanguage:'vendor/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN',
                        fancybox:'vendor/fancybox/source/jquery.fancybox.pack',
                        async:'vendor/async/lib/async'
                    },
                    shim: {
                        'highcharts': {
                            'deps': ['jquery'],
                            'exports': 'Highcharts'// Still gotta make this available //使用全局的Highcharts做为模块名称
                        },
                        'datetimepickerLanguage':{//因datetimepickerLanguage依赖datetimepicker,所以次代码是优先加载datetimepicker
                            'deps':['datetimepicker']
                        },
                        'datetimepicker':{
                            'deps':['css!vendor/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min']
                        },
                        'bootstrap':{
                            'deps':['jquery']
                        },
                        'async':{
                            'deps':[
                                'vendor/async/deps/nodeunit',
                                'css!vendor/async/deps/nodeunit.css'
                            ]
                        }
                    },
                    map: {
                        '*': {
                            'css': 'vendor/require-css/css'
                        }
                    }
                }
            }
        },
        concat: {
            css:{
                src:['app/css/**/*.css'],
                dest:'dest/css/main.css',
            }
        },
        uglify:{
            options:{
                banner:"/*! <%= pkg.name %> <%= grunt.template.today('yyyy-mm-dd') %> */\n"
            },
            build:{
                src:'dest/js/main.js',
                dest:'dest/js/main.min.js'
            }
        },
        cssmin:{
            css:{
                src:'dest/css/main.css',
                dest:'dest/css/main.min.css'
            }
        },
        copy: {
            main: {
                files:[
                    {expand: true, cwd: 'app/',src: 'indexProduct.html', dest: 'dest/'},
                    {expand: true, cwd: 'app/',src: 'favicon.ico', dest: 'dest/'},
                    {expand: true, cwd: 'app/',src: ['fonts/**'], dest: 'dest/'},
                    {expand: true, cwd: 'app/',src: ['images/**'], dest: 'dest/'},
                    {expand: true, cwd: 'app/',src: ['js/**/*.js'], dest: 'dest/'},
                    {expand: true, cwd: 'app/js',src: ['vendor/**'], dest: 'dest/js/'},
                ]
            },
        },
        rename: {
            main: {
                files: [
                    {src: ['dest/indexProduct.html'], dest: 'dest/index.html'},
                ]
            }
        },
        clean_other_js:{

        }
    });
    require('load-grunt-tasks')(grunt);
    grunt.registerTask('tools', ['less', 'react', 'browserSync', 'watch']);
    // grunt.registerTask('build',['copy','requirejs','concat','uglify','cssmin','rename','clean']);
    grunt.registerTask('default', ['concurrent']);
};